package com.huatai.datadevelop.thread.task;

import com.alibaba.fastjson.JSONObject;
import com.huatai.datadevelop.entity.*;
import com.huatai.datadevelop.service.*;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author hecai
 * @description: TODO
 * @date 2023/2/9 16:32
 * @Version 1.0
 */
@Slf4j
@Component
public class SuperviseTask {

    @Autowired
    private RealApiManageService realApiManageService;
    @Autowired
    private TableManageService tableManageService;
    @Autowired
    private ExecutService executService;
    @Autowired
    private ExecuteService executeService;
    @Autowired
    private InterfaceTableConfigService interfaceTableConfigService;
    @Autowired
    private JsonDataService jsonDataService;


    /**
     * 接口状态自检
     */
	@XxlJob("superviseTaskRunTask")
    public void runTask(){
		String params = XxlJobHelper.getJobParam();
        log.info("接口自检开始");
        long start = System.currentTimeMillis();
        List<RealApiManageEntity> list = realApiManageService.list();
        if(list!=null && list.size()>0){
            list.stream().forEach(p->{
                realApiManageService.asyncRequestApi(new JSONObject(),p);
            });
        }

        List<TableManageEntity> tableManageList = tableManageService.list();
        if(tableManageList!=null && tableManageList.size()>0){
            tableManageList.stream().forEach(table->{
                executService.asyncRequestApi(table);
            });
        }

        List<ExecuteEntity> executeList = executeService.list();
        if(executeList!=null && executeList.size()>0){
            executeList.stream().forEach(exec->{
                this.executeService.asyncRequestApi(new HashMap<>(),exec,new HashMap<>());
            });
        }

        List<InterfaceTableConfigEntity> interfaceTableConfig = interfaceTableConfigService.list();
        if(interfaceTableConfig!=null && interfaceTableConfig.size()>0){
            interfaceTableConfig.stream().forEach(interfaceTable->{
                this.interfaceTableConfigService.asyncRequestApi(new HashMap<>(),interfaceTable,new HashMap<>());
            });
        }

        List<JsonDataEntity> jsonDataEntityList = jsonDataService.list();
        if(jsonDataEntityList!=null && jsonDataEntityList.size()>0){
            jsonDataEntityList.stream().forEach(jsonData->{
                this.jsonDataService.asyncRequestApi(jsonData,new JSONObject());
            });
        }

        log.info("接口自检结束，共计耗时："+(System.currentTimeMillis()-start));
    }
}
